package com.irdstudio.apicenter.console.controller.authenticate;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.irdstudio.apicenter.console.controller.MacmsBaseAction;
import com.irdstudio.apicenter.console.dm.AppSaleSite;
import com.irdstudio.apicenter.console.service.authenticate.AppSaleSiteService;

@Controller
@RequestMapping(value = "/appSaleSite")
public class AppSaleSiteAction extends MacmsBaseAction {
	@Autowired
	private AppSaleSiteService appSaleSiteService;

	// 插入跳转
	@RequestMapping(value = "jumpAddSaleSite")
	public String jumpAddSaleSite(HttpServletRequest request) {
		String appid = request.getParameter("appid");
		request.setAttribute("appid", appid);
		return "auth/addSaleSite";
	}

	// 插入保存
	@RequestMapping(value = "addSaleSite")
	public void addSaleSite(
			@ModelAttribute("appSaleSite") AppSaleSite appSaleSite,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appSaleSiteService.insertOne(appSaleSite);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 更新跳转
	@RequestMapping(value = "jumpUpdateSaleSite")
	public String jumpUpdateSaleSite(HttpServletRequest request)
			throws SQLException, ClassNotFoundException {
		String saleSiteId = request.getParameter("saleSiteId");
		String appid = request.getParameter("appid");
		AppSaleSite appSaleSite = appSaleSiteService
				.queryAppTradePrivate(saleSiteId);
		request.setAttribute("appid", appid);
		request.setAttribute("appSaleSite", appSaleSite);
		return "auth/updateSaleSite";
	}

	// 更新保存
	@RequestMapping(value = "updateSaleSite")
	public void updateSaleSite(
			@ModelAttribute("appSaleSite") AppSaleSite appSaleSite,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appSaleSiteService.update(appSaleSite);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 删除
	@RequestMapping(value = "deleteSaleSite")
	public void deleteSaleSite(
			@ModelAttribute("appSaleSite") AppSaleSite appSaleSite,
			HttpServletRequest request, HttpServletResponse response)
			throws SQLException, ClassNotFoundException {
		int flag = -1;
		flag = appSaleSiteService.delete(appSaleSite);
		Map<String, Object> map = new HashMap<String, Object>(1);
		map.put("flag", flag);
		String jsonResult = JSON.toJSONString(map);
		PrintWriter printWriter = null;
		try {
			printWriter = response.getWriter();
			printWriter.print(jsonResult);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (null != printWriter) {
				printWriter.flush();
				printWriter.close();
			}
		}
	}

	// 查看
	@RequestMapping(value = "querySaleSiteDetail")
	public String viewDevice(HttpServletRequest request) throws SQLException,
			ClassNotFoundException {
		String saleSiteId = request.getParameter("saleSiteId");
		String appid = request.getParameter("appid");
		AppSaleSite appSaleSite = appSaleSiteService
				.queryAppTradePrivate(saleSiteId);
		request.setAttribute("appSaleSite", appSaleSite);
		request.setAttribute("appid", appid);
		return "auth/querySaleSiteDetail";
	}

}
